package com.example.demo.utils;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.concurrent.TimeUnit;

public class SeleniumUtils {

    public void sendKeys(WebDriver webDriver, String type, String element, String content) {
        switch (type) {
            case "id":
                webDriver.findElement(By.id(element)).sendKeys(content);
            case "name":
                webDriver.findElement(By.name(element)).sendKeys(content);
            case "xpath":
                webDriver.findElement(By.xpath(element)).sendKeys(content);
        }

    }

    public void submit(WebDriver webDriver, String element, String type, String content) {
        switch (type) {
            case "id":
                webDriver.findElement(By.id(element)).submit();
            case "name":
                webDriver.findElement(By.name(element)).submit();
            case "xpath":
                webDriver.findElement(By.xpath(element)).submit();
        }
    }

    public void click(WebDriver webDriver, String element, String type, String content) {
        switch (type) {
            case "id":
                webDriver.findElement(By.id(element)).click();
            case "name":
                webDriver.findElement(By.name(element)).click();
            case "xpath":
                webDriver.findElement(By.xpath(element)).click();
        }
    }

    public void clear(WebDriver webDriver, String element, String type, String content) {
        switch (type) {
            case "id":
                webDriver.findElement(By.id(element)).clear();
            case "name":
                webDriver.findElement(By.name(element)).clear();
            case "xpath":
                webDriver.findElement(By.xpath(element)).clear();
        }
    }

    public String getText(WebDriver webDriver, String element, String type) {
        String text;
        switch (type) {
            case "id":
                text = webDriver.findElement(By.id(element)).getText();
            case "name":
                text = webDriver.findElement(By.name(element)).getText();
            case "xpath":
                text = webDriver.findElement(By.xpath(element)).getText();
            default:
                text = null;
        }
        return text;
    }

    public void implicitlyWait(WebDriver webDriver, long time) {
        webDriver.manage().timeouts().implicitlyWait(time, TimeUnit.SECONDS);
    }

    public void chromeDriverMaxSize(WebDriver webDriver){
        webDriver.manage().window().maximize();
    }

    public void quit(WebDriver webDriver) {
        webDriver.quit();
    }

    public void get(WebDriver webDriver ,String url){
        webDriver.get(url);
    }

//    public void saveScreenshot(WebDriver webDriver, String fileName) {
//        File file = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
//        try {
//            FileUtils.copyFile(file, new File(fileName));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    public static void main(String[] args) {
        ChromeOptions options = new ChromeOptions();
        //允许所有请求
        options.addArguments("--remote-allow-origins=*");
        options.addArguments("--headless");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("www.baidu.com");
        SeleniumUtils seleniumUtils = new SeleniumUtils();
        seleniumUtils.sendKeys(webDriver, "xpath", "//*[@id\"kw\"]", "text");
    }


}
